import isFunctionComponent from "./isFunctionComponent";
import mountNativeElement from "./mountNativeElement"
export default function mountComponent(virtualDOM, container, oldDOM) {
    let nextVDOM = null
    let component = null
    if (isFunctionComponent(virtualDOM)) {
        nextVDOM = buildFunctionComponent(virtualDOM, container)
    } else {
        nextVDOM = buildClassComponent(virtualDOM, container)
        component = nextVDOM.component
    }

    if (isFunctionComponent(nextVDOM)) {
        nextVDOM = mountComponent(nextVDOM, container, oldDOM)
    } else {
        nextVDOM = mountNativeElement(nextVDOM, container, oldDOM)
    }
    if (component) {
        if (component.props && component.props.ref) {
            component.props.ref(component)
        }
        component.componentDidMount()
    }

}


function buildFunctionComponent(virtualDOM) {
    return virtualDOM.type(virtualDOM.props || {})
}


function buildClassComponent(virtualDOM) {
    const component = new virtualDOM.type(virtualDOM.props || {})
    const nextVDOM = component.render()
    nextVDOM.component = component
    return nextVDOM
}